Skip to main content

Events

BindAI provides event and callback mechanisms for observing and extending agent execution. There are two related but distinct event mechanisms:
  • Agent callbacksAgent.on() and Agent.emit() provide lightweight named callbacks directly on an agent.
  • Core eventsEventBus provides a structured event-publishing mechanism used by BindAI runtime components and integrations.
These mechanisms are useful for:
  • Logging
  • Monitoring
  • Analytics
  • Notifications
  • Debugging
  • Auditing
  • Custom integrations
  • Automation triggers
  • Application-specific execution behavior

Agent Events

The Agent class provides a lightweight named-event API. Register a callback with:
Emit an event with:
For example:
The callback receives the positional arguments supplied to emit(). Agent events are primarily useful for application-level callbacks and lightweight integrations.

Registering Callbacks

Use Agent.on() to register a callback for a named event.
The first argument is the event name. The second argument is the callback that should run when the event is emitted. Multiple callbacks can be registered for the same event.
When the event is emitted, the registered callbacks are invoked.

Emitting Events

Use Agent.emit() to emit a named event.
Callbacks registered for that event receive the emitted arguments. For example:
The event name and callback argument contract should remain consistent within an application.

Custom Events

Agent event names are strings, allowing applications to define their own event names.
Use descriptive event names that clearly communicate what happened. Application-specific event names should be documented together with their callback arguments.

Event Arguments

Agent.emit() accepts positional arguments after the event name. For example:
A callback can receive those arguments:
Output:
The callback signature must match the arguments emitted by the application.

Core EventBus

BindAI also provides a core EventBus abstraction.
An event bus allows components to subscribe to named events and publish structured Event objects. Create an event bus:
Subscribe to an event:
Publish an event:
This mechanism is separate from the Agent.on() / Agent.emit() callback API. The event bus is intended for communication between BindAI components and integrations that need a structured event boundary.

Event Objects

Core events are represented by Event objects.
An event can carry a payload:
The base event also provides framework-managed event metadata such as an identifier and timestamp. Applications can therefore use structured event objects instead of passing arbitrary positional arguments between core components.

Subscribing to EventBus Events

Use EventBus.subscribe() to register an event handler.
The handler receives the published Event object. Unlike Agent.on(), the callback does not receive arbitrary positional arguments supplied by the publisher. Instead, it receives the structured event.

Publishing EventBus Events

Publish an event with EventBus.publish().
The event bus finds handlers registered for the event name and invokes them. The event bus also supports a wildcard subscription:
A wildcard handler receives published events regardless of their specific event name.

Unsubscribing

EventBus handlers can be removed with unsubscribe().
This is useful when an integration or component no longer needs to receive events. A handler should generally be unsubscribed when the component owning the subscription is disposed or no longer active.

EventBus Handler Isolation

The core EventBus isolates handler failures from the publishing component. If a handler raises an exception, the event bus logs the failure rather than allowing the handler exception to interrupt the remaining event handling. Conceptually:
If one handler fails, the event bus records the failure and continues processing the other handlers. This is useful for optional integrations such as logging, metrics, notifications, and automation. Event handlers should nevertheless be kept small and reliable.

Agent EventBus

Each Agent has an event bus available through:
For example:
The agent can publish framework events through this event bus as part of its execution behavior. This provides a structured event boundary for integrations that need to observe agent activity.

Agent Callbacks vs EventBus

The two mechanisms serve different purposes. A useful rule is:
  • Use Agent.on() / Agent.emit() for lightweight application-level callbacks.
  • Use EventBus when components need to communicate through structured BindAI events.
  • Use automation triggers when an event should initiate an automation action.

Agent Lifecycle

Agent execution also has lifecycle behavior. The execution engine publishes framework events such as agent-started and agent-finished events through the core event infrastructure. Agent execution also exposes lifecycle callback methods:
These mechanisms should not be treated as interchangeable. Lifecycle callbacks are directly associated with execution lifecycle behavior, while the event bus provides a decoupled event boundary for event consumers. Do not assume undocumented event names or callback signatures.

Tool Events

Tool execution can also produce core events. The agent publishes a ToolExecutedEvent through its event bus after a tool execution. For example:
The exact event name should be obtained from the corresponding BindAI event type rather than assumed in application code. Tool events are useful for integrations such as:
  • Logging
  • Auditing
  • Metrics
  • Monitoring
  • Automation
  • Debugging
For detailed tool result behavior, see the Tool Results documentation.

Events and Automation

The core event system provides the foundation for event-driven automation. BindAI’s Automation package provides EventTrigger, which can listen to an EventBus and invoke a target when a matching event is published. The relationship is:
For example:
Once attached, the trigger listens for the configured event. When the matching event is published:
the automation target is invoked. This creates a clean boundary between event production and automation behavior.

Enabling and Disabling Automation Triggers

EventTrigger supports temporary enable/disable behavior.
While disabled, matching events do not invoke the target. It can be enabled again:
This is useful when an automation should remain attached to an event source but temporarily stop reacting to events.

Detaching Automation Triggers

A trigger can be detached from its event source:
After detaching, the trigger no longer receives events from that event bus. This allows applications to manage the lifetime of automation subscriptions explicitly.

Trigger Registry

BindAI Automation also provides TriggerRegistry.
Triggers can be registered by name:
They can then be retrieved:
The registry provides named management of automation triggers. It is useful when an application needs to manage multiple triggers independently.

Event-Driven Architecture

The event and automation layers can be combined into a larger architecture:
The important principle is that the component producing an event does not need to know which consumers will react to it. This reduces coupling between agents, integrations, monitoring systems, and automation.

Events and Workflows

Events can also act as boundaries around larger application workflows. For example:
Events should generally initiate or notify workflows rather than contain complex orchestration logic themselves. For explicit multi-step orchestration, use BindAI workflows.

Events and External Integrations

Events can connect BindAI execution to external application services. For example:
External service calls should generally be delegated to dedicated application components or BindAI connections rather than implemented as large event handlers.

Keeping Event Handlers Lightweight

Event handlers should generally perform small, focused operations. Good event handlers include:
  • Logging
  • Updating counters
  • Recording metrics
  • Creating audit records
  • Triggering lightweight notifications
  • Scheduling background work
  • Passing work to another application component
Avoid putting large amounts of business logic directly inside event callbacks. For complex processing, use an event as the boundary and delegate the work to another component.

Error Handling in Event Handlers

Event handlers are application code and can introduce their own errors. For EventBus handlers, BindAI isolates handler exceptions so that one failing handler does not prevent the event bus from continuing to other handlers. Applications should still implement appropriate logging and error handling within handlers when the operation itself is important. For example:
Optional monitoring or notification behavior should not unnecessarily complicate the primary agent execution path.

Event Naming

Use descriptive event names. Framework event names should come from the corresponding BindAI event types. For application-specific agent events, use names that clearly describe what happened:
For core EventBus events, prefer stable event names that identify the event type. Avoid ambiguous names that make it difficult for subscribers to understand what the event represents.

Hooks and Callbacks

Events are not the only extension mechanism available to agents. Agents also expose hooks and lifecycle callbacks. The hook() method allows application code to register hook behavior:
Agents also provide lifecycle callback methods such as:
These mechanisms are useful for execution-related behavior. The exact hook contract depends on the execution mechanism using the hook, so applications should follow the callback interface provided by the relevant BindAI API.

Events vs Hooks

Events and hooks serve related but different purposes. A simple rule is:
  • Use agent callbacks for lightweight local callbacks.
  • Use core events for structured communication between components.
  • Use automation triggers when events should initiate actions.
  • Use hooks for reusable execution-related behavior.
  • Use workflows for explicit multi-step orchestration.

Testing Events

Event behavior should be tested independently from external services. A simple agent callback test can verify that a callback receives emitted values:
A core event bus can be tested independently:
Automation triggers can then be tested at the integration boundary:
Tests should verify:
  • Event registration
  • Event emission
  • EventBus subscription
  • EventBus publication
  • Event arguments or payloads
  • Multiple subscribers
  • Handler isolation
  • Trigger attachment
  • Trigger detachment
  • Trigger enable/disable behavior
Avoid requiring external services when testing the core event mechanism.

Events and Observability

Events provide useful building blocks for observability. Applications can connect events to:
  • Logging
  • Metrics
  • Tracing
  • Auditing
  • Error monitoring
However, the event mechanism itself is not a complete observability platform. Observability systems may require additional infrastructure for:
  • Persistent event storage
  • Distributed tracing
  • Metrics aggregation
  • Dashboards
  • Alerting
  • Background processing
Treat BindAI events as an integration mechanism rather than assuming they provide a complete observability stack.

Event-Driven Applications

As an application grows, events can provide boundaries between independent components. For example:
This architecture allows the agent and other BindAI components to remain focused on their primary responsibilities while application integrations react independently.

Current Event Architecture

The current BindAI event architecture can be summarized as:
These layers are related, but they should not be treated as interchangeable APIs.

Best Practices

  • Use descriptive event names.
  • Keep application event contracts consistent.
  • Document callback arguments for custom agent events.
  • Use structured Event objects with EventBus.
  • Prefer framework event types when consuming BindAI core events.
  • Keep event handlers small and focused.
  • Keep heavy business logic outside event handlers.
  • Handle important handler failures appropriately.
  • Use EventTrigger when an event should initiate automation.
  • Detach triggers when they are no longer needed.
  • Use hooks for reusable execution-related behavior.
  • Use lifecycle callbacks for lifecycle-specific behavior.
  • Use workflows for complex orchestration.
  • Test event mechanisms independently from external services.
  • Do not assume undocumented lifecycle event names or callback signatures.
  • Do not treat the event system as a complete observability platform.

Summary

BindAI provides several complementary mechanisms for reacting to agent activity. At the agent level:
For structured core events:
For event-driven automation:
These mechanisms allow BindAI applications to connect agent execution with logging, monitoring, auditing, notifications, automation, workflows, and external services without tightly coupling those components to the agent itself. The key principle is to use the appropriate layer for the job: agent callbacks for lightweight local behavior, EventBus for structured event communication, automation triggers for event-driven actions, and workflows for complex orchestration.